package com.xzc.web;

import com.xzc.domain.User;
import com.xzc.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 徐州城
 * @version 1.0
 */
public class RegisterServlet extends HttpServlet {

    private UserService userService = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");

        //2、检查用户名是否可用
        if (userService.existUser(username)){
            System.out.println("用户名[" + username + "]已存在");
            //不可用，返回注册页面
            req.getRequestDispatcher("/pages/user/register.html").forward(req,resp);
        }else {
            //可用，调用Service保存数据到 数据库中，跳到注册成功页面
            userService.registerUser(new User(null,username,password,email));
            req.getRequestDispatcher("/pages/user/register_success.html").forward(req,resp);
        }

    }
}
